What's the best way to annotate this ggplot2 plot? [R]
Posted
by Matt Parker
on Stack Overflow
See other posts from Stack Overflow
or by Matt Parker
Published on 2010-05-12T18:53:25Z
Indexed on
2010/05/14
2:54 UTC
Read the original article
Hit count: 357
Here's a plot:
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = hp, group = factor(am), color = factor(am))) +
stat_smooth(fun.data = "mean_cl_boot", geom = "pointrange") +
stat_smooth(fun.data = "mean_cl_boot", geom = "line") +
geom_hline(yintercept = 130, color = "red") +
annotate("text", label = "130 hp", x = .22, y = 135, size = 4)
I've been experimenting with labeling the geom_hline in a few different ways, each of which does something I want but has a problem that the other methods don't have. annotate()
, used above, is nice - the text is resizeable, black, and easy to position. But it can only be placed within the plot itself, not outside the plot like the axis labels. It also makes an "a" appear in the legend, which I can't dismiss with legend = FALSE
.
legend = FALSE
works with geom_text
, but I can't get geom_text to just be black - it seems to be getting tangled up in the line colorings.
grid.text
lets me put the text anywhere I want, but I can't seem to resize it.
I can definitely accept the text being inside of the plot area, but I'd like to keep the legend clean. I feel like I'm missing something simple, but I'm just fried. Thanks in advance for your consideration.
© Stack Overflow or respective owner